cc.Class({
    extends: cc.Component,

    properties: {

    },

    onLoad() {
        cc.GameProxy = this;
        AppCall.keepAlive();
        if(cc.Toast.loading) {
            GameToast.showLoading = cc.Toast.loading;
            GameToast.showMessage = cc.Toast.message;
        } else {
            cc.Toast.loading = GameToast.showLoading;
            cc.Toast.message = GameToast.showMessage;
        }
        cc.Emitter.on(EVENTS.GET_TOKEN, this.onGetToken, this);
    },

    onGetToken() {
        this.loginCbk && this.loginCbk();
    },

    getAccessToken(game, cbk) {
        LINK.gameType = game;
        this.loginCbk = cbk;
        var onToken = function(token) {
            cc.Toast.loading(true);
            cc.Toast.loading(false);
            LINK.accessToken = token;
            cbk && cbk();
        }
        if (!cc.sys.isMobile) {
            var params = cc.Global.getUrlParams();
            if(params.token) {
                onToken(params.token);
            } else {
                cc.Proxy.getToken(params => onToken(params.token));
            }
        } else {
            AppCall.getAppData(params => {
                LINK.uuid = params.uuid;
                LINK.token = params.token;
                LINK.userID = params.UserID;
                LINK.isGuestLogin = params.userSource == 0;
                onToken(params.token);
            });
        }
    },

    callLogin() {
        if (!cc.sys.isMobile) {
            cc.log('........call login........');
        } else {
            AppCall.gameLogin();
        }
    },

    userBind() {
        if (!cc.sys.isMobile) {
            cc.log('........user bind........');
        } else {
            AppCall.call('userBind');
        }
    },

    getUserInfo(cbk) {
        cc.Linker(URL.UserInfo).showLoading(false).showMessage(false).request(data => {
            data.head = data.headImg;
            data.amount = data.useAmount;
            cbk && cbk(data);
        });
    },

    buryingPoint(type, cbk) {
        cc.Linker(URL.BuryingPoint, {
            buryingType: type,
            gameType: LINK.gameType,
        }).showMessage(false).showLoading(false).request(cbk, cbk);
    },

    openShop(openCbk, closeCbk) {
        cc.Popup.show('public/ShopPop', openCbk, closeCbk);
    },

    gameCharge(data, OKcbk, NOcbk) {
        if (!cc.sys.isMobile) {
            setTimeout(function() {
                cc.Toast.loading(false);
                OKcbk && OKcbk();
                cc.log('........game charge........');
            }, 1000);
        } else {
            var syncState = function () {
                AppCall.gameBack(function (result) {
                    var data = JSON.parse(result);
                    if (data.GameRechargeState == 1) {
                        cc.Toast.loading(false);
                        if (data.GameRechangeSuccess == 1) {
                            OKcbk && OKcbk(data);
                            cc.Toast.message(LangPublic.W005);
                            cc.Notification.emit('Refresh_UserInfo');
                        } else {
                            NOcbk && NOcbk();
                            cc.Toast.message(LangPublic.W006);
                        }
                    } else {
                        setTimeout(syncState, 1000);
                    }
                });
            };
            AppCall.gameRecharge(data);
            setTimeout(syncState, 1000);
        }
    },

    backHome() {
        if (!cc.sys.isMobile) {
            cc.log('........back home........');
        } else {
            AppCall.backHome();
        }
    }
});
